package game.controller
{
	import flash.utils.getTimer;
	import game.constants.ApplicationNotification;
	import game.model.building.BuildingConfigProxy;
	import game.model.building.BuildingProxy;
	import game.model.building.vo.BuildingConfigVO;
	import game.model.building.vo.BuildingVO;
	import game.model.player.PlayerProxy;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.command.SimpleCommand;
	
	/**
	 * ...
	 * @author Thot
	 */
	
	public class AddBuildingCommand extends SimpleCommand
	{
		override public function execute( notification:INotification ):void
		{
			var configProxy:BuildingConfigProxy = facade.retrieveProxy(BuildingConfigProxy.NAME) as BuildingConfigProxy;
			var proxy:BuildingProxy = facade.retrieveProxy(BuildingProxy.NAME) as BuildingProxy;
			var configVO:BuildingConfigVO = configProxy.getBuildingConfigBy( "id", notification.getBody().id );
			var buildVO:BuildingVO = new BuildingVO();
			
			var playerProxy:PlayerProxy = facade.retrieveProxy(PlayerProxy.NAME) as PlayerProxy;
			
			buildVO.id 				= configVO.id;
			buildVO.name 			= notification.getBody().name;
			buildVO.level 			= 0;
			buildVO.nextLevelTime 	= configVO.nextLevelTime[0];
			buildVO.prodMana 		= configVO.prodMana[0];
			buildVO.prodGold 		= configVO.prodGold[0];
			buildVO.costGems 		= configVO.costGems[0];
			buildVO.costGold 		= configVO.costGold[0];
			buildVO.costMana 		= configVO.costMana[0];
			
			if ( 
				playerProxy.currentPlayer.gold < buildVO.costGold || 
				playerProxy.currentPlayer.mana < buildVO.costMana || 
				playerProxy.currentPlayer.gems < buildVO.costGems ||
				buildVO.isIncreasing()
			)
			{
				sendNotification(ApplicationNotification.CANNOT_BUY);
				return;
			}
			
			playerProxy.currentPlayer.gold -= buildVO.costGold;
			playerProxy.currentPlayer.mana -= buildVO.costMana;
			playerProxy.currentPlayer.gems -= buildVO.costGems;
			
			proxy.addBuilding(buildVO);
		}
	}

}